﻿using System;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace WcfExample
{
    /// <summary>
    /// Dummy WCF example for testing binary protocol sniffer
    /// Loosely based on http://www.codeproject.com/KB/WCF/wcf_bohansen.aspx
    /// </summary>

    public class WcfExample : IDisposable
    {
        public WcfExample()
        {
            StartServer();
            ConnectClient();
        }

        public void Dispose()
        {
            StopServer();
            (clientProxy as IDisposable).Dispose();
        }
                
        public string Invoke(string input)
        {
            return clientProxy.Echo(input);
        }

        private void ConnectClient()
        {
            // create client proxy
            var endpointAddress = new EndpointAddress(serviceUrl);
            clientProxy = ChannelFactory<IService1>.CreateChannel(GetBinding(), endpointAddress);
        }

        private Binding GetBinding()
        {
            var binaryMEBE = new BinaryMessageEncodingBindingElement();
            var httpTransport = new HttpTransportBindingElement() { AuthenticationScheme = AuthenticationSchemes.Anonymous };
            return new CustomBinding(binaryMEBE, httpTransport);
        }

        private void StartServer()
        {
            // set up service
            serviceHost = new ServiceHost(typeof(Service1));
            serviceHost.AddServiceEndpoint(typeof(IService1), GetBinding(), serviceUrl);

            // set up metadata service describing the main service
            ServiceMetadataBehavior metadataBehavior;
            metadataBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (metadataBehavior == null)
            {
                metadataBehavior = new ServiceMetadataBehavior();
                metadataBehavior.HttpGetUrl = new Uri(serviceMetaUrl);
                metadataBehavior.HttpGetEnabled = true;
                serviceHost.Description.Behaviors.Add(metadataBehavior);
            }

            serviceHost.Open();
        }

        private void StopServer()
        {
            serviceHost.Close();
        }

        private ServiceHost serviceHost = null;
        IService1 clientProxy = null;
        private string serviceUrl = "http://ipv4.fiddler:54321/MyService";
        private string serviceMetaUrl = "http://ipv4.fiddler:54322/MyService";



        /// <summary>
        /// classes representing the WCF service contract
        /// </summary>
        [ServiceContract()]
        public interface IService1
        {
            [OperationContract]
            string Echo(string input);
        }

        public class Service1 : IService1
        {
            public string Echo(string input)
            {
                return String.Format("Hello from WCF - your input was: " + input);
            }
        }
    }
}
